package com.wanxi.springboot.team.manage.system.service.impl;

import com.wanxi.springboot.team.manage.system.api.CommonResult;
import com.wanxi.springboot.team.manage.system.mapper.PermissionMapper;
import com.wanxi.springboot.team.manage.system.mapper.RoleMapper;
import com.wanxi.springboot.team.manage.system.model.*;
import com.wanxi.springboot.team.manage.system.mapper.RolePermissionMapper;
import com.wanxi.springboot.team.manage.system.service.RolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 蒋帆
 * @since 2020-12-21
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {

    @Autowired(required = false)
    RolePermissionMapper rolePermissionMapper;
    @Autowired(required = false)
    PermissionMapper permissionMapper;
    @Autowired(required = false)
    RoleMapper roleMapper;

    @Override
    public CommonResult updateRolePermission(Map map) {
        try {
            //获取角色id
            Integer roleId = Integer.valueOf((String) map.get("roleId"));
            //获取移除的权限id
            String left = ((String) map.get("left")).replace("\"", "");
            if (!left.equals("[]")) {
                String[] leftSplit = left.substring(1, left.length() - 1).split(",");
                System.out.println(leftSplit);
                List<Integer> leftList = new ArrayList<>();
                for (String str : leftSplit) {
                    int num = Integer.parseInt(str);
                    leftList.add(num);
                }
                if (leftList.size() > 0) {//移除角色所拥有的权限
                    rolePermissionMapper.updateRolePermission(leftList, roleId, "no");
                }
            }
            //获取放入角色的权限id
            String right = ((String) map.get("right")).replace("\"", "");
            if (!right.equals("[]")) {
                String[] rightSplit = right.substring(1, right.length() - 1).split(",");
                System.out.println(rightSplit);
                List<Integer> addRightList = new ArrayList<>();
                List<Integer> editRightList = new ArrayList<>();
                for (String str : rightSplit) {
                    int num = Integer.parseInt(str);
                    //判断角色权限表中该用户曾经是否存在该权限
                    if (rolePermissionMapper.haveRolePermission(num, roleId))
                        editRightList.add(num);//如果有，则直接修改标识为不可用
                    else
                        addRightList.add(num);//如果没有则增加该权限
                }
                if (editRightList.size() > 0)
                    rolePermissionMapper.updateRolePermission(editRightList, roleId,"yes");
                if (addRightList.size() > 0)
                    rolePermissionMapper.addRolePermission(addRightList, roleId);
            }
            //添加调组记录
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("分配失败");
        }
        return CommonResult.success(true, "分配成功");
    }
}
